package com.artisan.web.action;

import com.alibaba.fastjson.JSON;
import com.artisan.common.pojo.ApiRes;
import com.artisan.dao.AddressMapper;
import com.artisan.medium.wx.pojo.AccessToken;
import com.artisan.medium.wx.service.WxAddressService;
import com.artisan.medium.wx.service.WxMessageService;
import com.artisan.medium.wx.service.WxUserService;
import com.artisan.model.Address;
import com.artisan.model.Customer;
import com.artisan.model.CustomerMessage;
import com.artisan.web.pojo.AddressMapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by yang on 2017/6/24.
 */
@Controller
@RequestMapping("wx/add/")
public class WxAddressController {
    @Autowired
    private WxUserService wxUserService;
    @Autowired
    private WxAddressService wxAddressService;
    @RequestMapping("address_list")
    public ModelAndView address_list(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Customer customer=(Customer)request.getSession().getAttribute("USER");
        Map<String,Object>map=wxAddressService.getAddressByUid(customer.getId());
        return new ModelAndView("address_list",map);
    }

    @RequestMapping("address_edit")
    public ModelAndView address_edit(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id=Integer.valueOf(String.valueOf(request.getParameter("id")));
        Map<String, Object> resultData = new HashMap<String, Object>();
        Address address=wxAddressService.getAddressById(id);
        resultData.put("address",address);
        return new ModelAndView("address_edit",resultData);
    }
    @RequestMapping("address_add")
    public ModelAndView address_add(HttpServletRequest request, HttpServletResponse response) throws Exception {
        return new ModelAndView("address_add");
    }
    @ResponseBody
    @RequestMapping(value="delAddress",method = RequestMethod.GET)
    public String delAddress(int id){
        Map<String, Object> resultData = new HashMap<String, Object>();
        wxAddressService.delAddress(id);
        resultData.put("result",ApiRes.SUCCESS_CODE);
        return JSON.toJSONString(resultData);
    }

    @ResponseBody
    @RequestMapping(value = "defaultAddress",method = RequestMethod.GET)
    public String defaultAddress(HttpServletRequest request, HttpServletResponse response){
        System.out.println("join:defaultAddress.do");
        System.out.println(request.getSession().getAttribute("USER"));
        int id=Integer.valueOf(String.valueOf(request.getParameter("id")));
        Map<String,Object> map=new HashMap<String,Object>();
        Customer customer=null;
        if( request.getSession().getAttribute("USER")!=null){
            customer=(Customer) request.getSession().getAttribute("USER");
            wxAddressService.defaultAddress(id,customer.getId());
        }


        map.put("result",ApiRes.SUCCESS_CODE);
        return JSON.toJSONString(map);
    }

    @ResponseBody
    @RequestMapping(value = "editAddress",method =RequestMethod.GET)
    public String editAddress(AddressMapping addressMapping)  {
        Map<String, Object> resultData = new HashMap<String, Object>();
        Address address=new Address();
        address.setId(addressMapping.getId());
        address.setuName(addressMapping.getuName());
        address.setAddress(addressMapping.getAddress());
        address.setAname(addressMapping.getCitys()+" "+addressMapping.getAddress());
        address.setCitys(addressMapping.getCitys());
        address.setAphone(addressMapping.getAphone());
        String[] ads = addressMapping.getCitys().split(" ");
        String provinceId = null;
        String cityId = null;
        String areaId = null;
        for (int i = 0; i < ads.length; i++) {
            System.out.println(ads[i]);
            if (i == 0) {
                provinceId = wxAddressService.getAddIdByName(ads[i]);
            } else if (i == 1) {
                cityId = wxAddressService.getAddIdByName(ads[i]);
            } else {
                ads[i] = ads[i].replace("区", "");
                areaId = wxAddressService.getAddIdByName(ads[i]);
            }
        }
        System.out.println(provinceId);
        System.out.println(cityId);
        System.out.println(areaId);
        address.setProvinceId(provinceId);
        address.setCityId(cityId);
        address.setAreaId(areaId);
        wxAddressService.editAddress(address);
        resultData.put("result",ApiRes.SUCCESS_CODE);
        return JSON.toJSONString(resultData);
    }

    @ResponseBody
    @RequestMapping(value = "addAddress",method =RequestMethod.GET)
    public String addAddress(AddressMapping addressMapping,HttpServletRequest request)  {
        Map<String, Object> resultData = new HashMap<String, Object>();
        Customer customer=null;
        if( request.getSession().getAttribute("USER")!=null){
            customer=(Customer) request.getSession().getAttribute("USER");
        }
        Address address=new Address();
        address.setId(addressMapping.getId());
        address.setuName(addressMapping.getuName());
        address.setAddress(addressMapping.getAddress());
        address.setAname(addressMapping.getCitys()+" "+addressMapping.getAddress());
        address.setCitys(addressMapping.getCitys());
        address.setAphone(addressMapping.getAphone());
        String[] ads = addressMapping.getCitys().split(" ");
        String provinceId = null;
        String cityId = null;
        String areaId = null;
        for (int i = 0; i < ads.length; i++) {
            System.out.println(ads[i]);
            if (i == 0) {
                provinceId = wxAddressService.getAddIdByName(ads[i]);
            } else if (i == 1) {
                cityId = wxAddressService.getAddIdByName(ads[i]);
            } else {
                ads[i] = ads[i].replace("区", "");
                areaId = wxAddressService.getAddIdByName(ads[i]);
            }
        }
        System.out.println(provinceId);
        System.out.println(cityId);
        System.out.println(areaId);
        address.setProvinceId(provinceId);
        address.setCityId(cityId);
        address.setAreaId(areaId);
        address.setuId(customer.getId());
        wxAddressService.addAddress(address);
        resultData.put("result",ApiRes.SUCCESS_CODE);
        return JSON.toJSONString(resultData);
    }
}
